package com.along.game.action.manage;

import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;

import com.along.framework.utils.FgConstant;
import com.along.framework.utils.PatinationUtil;
import com.along.framework.web.BaseAction;

import com.along.game.model.Music;
import com.along.game.service.GameConfigService;

public class MusicAction  extends BaseAction{

	/**
	 * 
	 */
	private static final long serialVersionUID = -4012731035103016258L;

	
	
	@Autowired
	private GameConfigService gameConfigService;
	private Music  musicVO;
	private Map if_using =new LinkedHashMap(); 



	public Music getMusicVO() {
		return musicVO;
	}


	public void setMusicVO(Music musicVO) {
		this.musicVO = musicVO;
	}

	public Map getIf_using() {
		return FgConstant.IF_USING;
	}


	public void setIf_using(Map if_using) {
		this.if_using = if_using;
	}


	/**
	 * 查询音乐列表
	 */
	public String queryMusic()
	{
	       HttpServletRequest request=getRequest();
			String currentpage=request.getParameter("currentpage");
	        int  totalRows =gameConfigService.queryMusicCount(musicVO);
			currentpage=PatinationUtil.getCurrentPage(currentpage);
			int startrow=PatinationUtil.getStartRow(currentpage);
			int endrow=PatinationUtil.getEndRow(currentpage);
			List musicList =gameConfigService.queryMusic(startrow, endrow, musicVO);
			currentpage=PatinationUtil.getCurrentPage(currentpage);
            request.setAttribute("totalRows", totalRows);
			request.setAttribute("currentpage", currentpage);
			request.setAttribute("musicList", musicList);
	
return "/gameconfig/musiclist.jsp";
	}
	
	
	/**
	 * 进入音乐修改页
	 */
	public String editMusic()
	{
	       HttpServletRequest request=getRequest();
		
			
			
			 if(musicVO!=null)
			  {
				  request.setAttribute("addorupdate", "update");
				  musicVO=gameConfigService.queryMusicDetail(String.valueOf(musicVO.getId()));
				
			  }
			  else{
		
				  request.setAttribute("addorupdate", "add");
			  }
				 
			
				
return "/gameconfig/musicedit.jsp";
	}
	
	
	
	/**
	 * 执行修改、增加音乐
	 * @return
	 * @throws IOException 
	 * @throws BusException
	 */
	public String postMusic() throws IOException{
	
	
	 HttpServletRequest request=getRequest();
 HttpServletResponse response=getResponse();
 String addorupdate=request.getParameter("addorupdate");
		String message="";
		if(addorupdate.equals("add"))
		{
			if(gameConfigService.loadMusic(musicVO)>0)
				message="添加成功";
			else
				message="添加失败";
				
		}
		else if(addorupdate.equals("update"))
		{
		
			if(gameConfigService.saveMusic(musicVO)>0)
				message="修改成功";
			else
				message="修改失败";
			
		}
		else  if(addorupdate.equals("remove")){
	
			gameConfigService.removeMusic(String.valueOf(musicVO.getId()));
		
		
		message="删除成功";
		}
		 response.setContentType("text/html;charset=UTF-8");
		response.getWriter().println("<script>alert('"+message+"');this.location.href='music_queryMusic.do';</script>");
		response.flushBuffer();
		  return  null;
	
		//JSONUtil.writerJSON(list, response.getWriter(),java.util.Date.class);

	}
	
	
}
